package com.xcl.controller;


import com.xcl.pojo.Centre;
import com.xcl.service.CentreServiceInter;
import com.xcl.service.XclTailAfterServiceInter;
import com.zzg.entity.Delivery;
import com.zzg.entity.Driver;
import com.zzg.entity.Tailafter;
import com.zzg.entity.TmsUser;
import com.zzg.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName CentreController
 * @Description
 * @Author: xiaochaolei
 * @date 2021/7/2 15:31
 * @Version 1. 0
 */
@Controller
public class CentreController {

    @Autowired
    CentreServiceInter centreServiceInter;

    @Autowired
    XclTailAfterServiceInter xclTailAfter;
    /**
     * 得到所有运单状态为运输中的数据
     * @param page 页码数
     * @param limit 条目数
     * @param waybill_id 运单号
     * @param sub_name 到达分站
     * @param modeTransport 送货方式
     * @param TypeTransportation 运输类型
     * @param remark 地址
     * @param id 判断是否为高级查询
     * @return 数据集合
     */
    @GetMapping(value = "/getAllCentre", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> getAllCentre(int page,int limit,String waybill_id, String sub_name, String modeTransport, String TypeTransportation, String remark,String id){
        Map<String,Object> map=new HashMap<>();
        map.put("code","0");
        map.put("msg","0");
        if (id==null){
            map.put("count",centreServiceInter.getAllCentre().size());
            map.put("data",centreServiceInter.getAllCentreByLimit(page,limit));
            return map;
        }else {
            System.out.println(TypeTransportation+"--"+modeTransport+"--"+sub_name);
            map.put("count",centreServiceInter.getCentreByWhere(waybill_id,sub_name,modeTransport,TypeTransportation,remark,page,limit).size());
            map.put("data",centreServiceInter.getCentreByWhere(waybill_id,sub_name,modeTransport,TypeTransportation,remark,page,limit));
            return map;
        }
    }

    /**
     * 修改状态为派送中
     * @param state 封装好的数据
     */
    @PostMapping (value = "/updateState", produces = "application/json;charset=utf-8")
    @ResponseBody
    public void updateState(String state){
        //根据字符串分割
        String[] split= state.split("-");
        //循环选择跟踪信息
        for (int i = 0; i < split.length; i++) {
            Tailafter tailafter=new Tailafter();
            //设置跟踪信息的id
            tailafter.setTailafter(UuidUtil.getUuid());
            //获得当前时间
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String hehe = dateFormat.format( now );
            //设置跟踪时间
            tailafter.setTailafterTime(hehe);
            //设置运单号
            tailafter.setWaybillId(split[i]);
            //设置状态
            tailafter.setState("派送中");
            //选择跟踪信息
            xclTailAfter.insertXclTailAfter(tailafter);
        }
        //修改
        centreServiceInter.upteModification(split);
    }

    /**
     * 根据id查询货运信息
     * @param freightInformation 货运id
     * @return 货运信息
     */
    @PostMapping (value = "/getFreightinformationTableByid", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Centre getFreightinformationTableByid(String freightInformation){
        Centre freightinformationTableByid = centreServiceInter.getFreightinformationTableByid(freightInformation);
        return freightinformationTableByid;
    }

    /**
     * 新增订单派送
     * @param delivery 订单派送信息
     * @param driver 车辆信息
     */
    @PostMapping (value = "/insertDelivery", produces = "application/text;charset=utf-8")
    @ResponseBody
    public String insertDelivery( Driver driver, String waybill_id, HttpServletRequest request,Delivery delivery,String fenzhan){
        //设置分站
        delivery.setSubstation(fenzhan);
        //得到操作员
        TmsUser tms_user =(TmsUser) request.getSession().getAttribute("TMS_User");
       //设置到派送信息去
        delivery.setOperator(tms_user.getTmsName());
        //调用方法
        centreServiceInter.insertDelivery(delivery,driver,waybill_id);
        return "成功";
    }

}
